<?php
if($_REQUEST['run']='setting')
// function upload sound file
if(isset($_POST['submit'])){
	if(checknSound()){
		//limit sound file
		$limit = 0.5;
		// file format
		$allowedExts = array( "wav","mp3");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if (in_array($extension, $allowedExts)){
			if ($_FILES["file"]["error"] > 0)
			{
				echo '<script>
								alert("Error: " '. $_FILES["file"]["error"] .' "<br>");
	 							reLoad1();
	 					<script>';

			}else{
				if($_FILES["file"]["size"] > $limit*1024*1024){
					echo '<script>
	 							alert("File over range,sound file < '.$limit.' MB");
	 							reLoad1();
	 						</script>';
				}
				else{
					if($_FILES["file"])
					if (file_exists("plugins/nms/upload/" . $_FILES["file"]["name"])){
						echo' <script>
									alert("'.$_FILES["file"]["name"] .' File is existed. ");
	 								reLoad1();
								</script>';
					}else{
						move_uploaded_file($_FILES["file"]["tmp_name"],"plugins/nms/upload/" . $_FILES["file"]["name"]);
						db_execute('Insert into plugin_nms_setting(sound_name,sound_source)
	 								Values ("'.$_FILES['file']['name'].'","plugins/nms/upload/")');
						// refresh page
						echo '<script>
	 									alert("Upload has successed");
	 									reLoad1();
	 								</script>';
					}
				}
			}
		}else{
			echo '<script>
	 										alert("File format is not correct");
	 										reLoad1();
	 									</script>';
		}
	}
	else{
		echo '<script>
	 										alert("Server is full, please delete any old sound to upload new sound");
	 										reLoad1();
	 								</script>';
	}

}

// function update database from host_snmp_cache to plugin_nms_interface
if(isset($_POST['update'])){
	// time limit
	set_time_limit(1000);
	$check = false;
	if($_POST['action']  == 'Fupdate'){
		$num = db_fetch_cell('Select count(*) from plugin_nms_interface');
		$host_name = db_fetch_assoc('Select a.hostname as ip_host_a ,a.description as des_host_a ,b.hostname as ip_host_b ,b.description as des_host_b  from host a ,host b
										 where a.hostname =b.hostname and a.id != b.id group by a.hostname ,b.hostname;');
		foreach ($host_name as $set){
			echo '<script>
	 					alert("Duplicate host :'.$set['des_host_a'].'('.$set['ip_host_a'].'),host :'.$set['des_host_b'].'('.$set['ip_host_b'].') !Please check host");
	 				</script>';
			$check= true;
		}
		if($num < 1 && $check ==false){
			$count = 0;
			$host = db_fetch_assoc('Select id from host where hostname != "127.0.0.1"');
			foreach($host as $item){
				$count++;
				$result = query_snmp_host($item['id'],1);
			}
			add_all_interface_into_table();
			add_groupdefault();
			add_interface_to_groupdefault();
			cacti_log('Network Monitoring System: had query '.$count.' host successed.');
			echo'
	 				<script>
	 					alert("First update successed !");
						reLoad1();
	 				</script>';
		}else{
			if($check==true){
				echo'
	 				<script>
	 					alert("First update not successed !");
						reLoad1();
	 				</script>';
			}else{
				echo'
	 				<script>
	 					alert("First update has performed !");
						reLoad1();
	 				</script>';
			}
		}

	}else{
		if($_POST['action']  == 'Iupdate'){
			//check if plugin_nms_interface has record
			$numin = db_fetch_cell('Select count(*) from plugin_nms_interface');
			if($numin >0){
				$host = db_fetch_assoc('Select id from host where hostname != "127.0.0.1"');
				$count1 = 0;
				foreach($host as $item1){
					update_value_all_interface($item1['id']);
					$interface = db_fetch_assoc('Select interface_id from plugin_nms_interface where host_id ='.$item1['id'].' and interface_alias="" and nms_status="Down"');
					foreach($interface as $item2){
						update_action_pause1($item2['interface_id']);
					}
					$count1++;
				}
				// Insert log
				$user_id= db_fetch_cell("select username from user_auth where id=" . $_SESSION["sess_user_id"]);
				$numlog = db_fetch_cell('SELECT count(*) FROM plugin_nms_log');
				if($numlog <1){
					add_all_log_into_table($user_id);
				}
				$numhostlog = db_fetch_cell('SELECT count(*) FROM plugin_nms_hostlog');
				if($numhostlog<1){
					add_all_hostlog();
				}
				echo'
	 				<script>
	 				alert("Interface information has updated successfull !");
					reLoad1();
	 				</script>';
			}else{
				echo'
	 				<script>
	 					alert("Choose first update, please !");
						reLoad1();
	 				</script>';
			}
		}

	}
}
if(isset($_POST['updatedevice']))
{
	$id_host = $_POST['id_host_setting'];
	$result = query_snmp_host($id_host,1);
	$result_id = db_fetch_cell('Select host_id from plugin_nms_interface where host_id = '.$id_host.' group by host_id');
	$user= db_fetch_cell("select username from user_auth where id=" . $_SESSION["sess_user_id"]);
	if(!empty($result_id)){
		//function get value not in table plugin_nms_interface
		$result_snmp=db_fetch_assoc('SELECT  snmp_index
						FROM host_snmp_cache  where field_value like "ethernetCsmacd%"  and host_id ="'.$id_host.'"and snmp_index
						NOT IN (SELECT snmp_index  from plugin_nms_interface where host_id="'.$id_host.'" and present=1 )');
		//function get value not in table host_snmp_cache
		$result_snmp1=db_fetch_assoc('SELECT snmp_index  from plugin_nms_interface where host_id="'.$id_host.'"and present=1 and snmp_index
						NOT IN (SELECT  snmp_index
						FROM host_snmp_cache  where field_value like "ethernetCsmacd%"  and host_id ="'.$id_host.'")');
		if(!empty($result_snmp)){
			$snmp_add ="";
			$counttest=0;
			add_one_hostlog($id_host);
			foreach ($result_snmp as $set){
				add_interface_into_table($id_host,$set['snmp_index']);
				update_value_interface($id_host,$set['snmp_index']);
				add_log_into_table($user,$id_host,$set['snmp_index']);
				$id_interface = db_fetch_cell("SELECT interface_id from plugin_nms_inteface where host_id ='.$id_host.' and snmp_index = ".$set['snmp_index']."");
				$id_group_interface = db_fetch_cell('SELECT interface_group_id from plugin_nms_interfacegroup where interface_id = '.$id_interface.'');
				if(empty($id_group_interface)){
					add_one_interface_to_groupdefault($id_interface);
				}
				$snmp_add= $snmp_add . "" .$set['snmp_index']."". " , ";
				$counttest++;
			}
			echo'
					 				<script>
					 				alert("'.$snmp_add.' '.$counttest.'Interface be added successfully !");
					 				</script>';
		}else{
			echo'
					 				<script>
					 				alert("Interface not really change !");
					 				</script>';
		}
		//update present = 0
		if(!empty($result_snmp1))
		{
			$snmp_present ="";
			foreach ($result_snmp1 as $set1){
				update_present_interface($id_host,$set1['snmp_index']);
				$snmp_present= $snmp_present . "" .$set1['snmp_index']."". " , ";
			}
			echo'
					 				<script>
					 				alert("'.$snmp_present.' Interfaces have to stop following  !");
					 				</script>';
		}
		else{
			echo'
					 				<script>
					 				alert("Interface does not have to stop following !");
					 				</script>';
		}
		update_value_all_interface_1($id_host);
		echo '<script>
					 			reLoad1();
					 		</script>';
	}
	elseif(empty($result_id)){
		$check=false;
		$host_name = db_fetch_assoc('Select a.hostname as ip_host_a ,a.description as des_host_a ,b.hostname as ip_host_b ,b.description as des_host_b  from host a ,host b
												 where a.hostname =b.hostname and a.id != b.id group by a.hostname ,b.hostname;');
		foreach ($host_name as $set){
			echo '<script>
							 					alert("Duplicate host :'.$set['des_host_a'].'('.$set['ip_host_a'].'),host :'.$set['des_host_b'].'('.$set['ip_host_b'].') !Please check host");
							 				</script>';
			$check= true;
		}
		if($check==false){
			add_host_interface_into_table($id_host);
			update_value_all_interface($id_host);
			add_log_host_into_table($user,$id_host);
			add_one_hostlog($id_host);
			$interface = db_fetch_assoc('Select interface_id from plugin_nms_interface where host_id ='.$id_host.' and interface_alias="" and nms_status="Down"');
			foreach($interface as $set2){
				update_action_pause1($set2['inerface_id']);
			}
			$id_interface = db_fetch_assoc('SELECT interface_id from plugin_nms_inteface where host_id ='.$id_host.'');
			foreach ($id_interface as $item){
				add_one_interface_to_groupdefault($item['interface_id']);
			}
			echo'
					 		<script>
					 			alert("Interface information has updated successfull !");
					 		</script>';
		}
		echo '<script>
					 			reLoad1();
					 		</script>';
	}
}
// function upload file to server
if(isset($_POST['save'])){
	$up = $_POST["up"];
	$down = $_POST["down"];
	$ddown = $_POST["ddown"];
	// Update data to setting
	update_sound_setting($up,$down,$ddown);
	echo '<script>
			 			reLoad1();
			 		</script>';
		
		
}
?>
